package com.qingzhuge.manager.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zeroxiao
 * @date 2019/9/11 09:06
 */
@Getter
@AllArgsConstructor
public class SecurityUser implements UserDetails {

    private static final long serialVersionUID = -7541095551580996341L;
    private final Long id;
    private Boolean root;
    private final String username;
    @JsonIgnore
    private final String password;
    private final String avatar;
    private final String email;
    private final String mobile;
    private final Long deptId;
    @JsonIgnore
    private final Collection<GrantedAuthority> authorities;
    private Date createTime;
    @JsonIgnore
    private boolean accountNonExpired;
    @JsonIgnore
    private boolean accountNonLocked;
    @JsonIgnore
    private boolean credentialsNonExpired;
    @JsonIgnore
    private boolean enabled;
    public Set<String> getRoles() {
        if (null == authorities){
            return new HashSet<>();
        }
        return authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
    }
}